package com.example.hzq.zqb.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.hzq.zqb.R;
import com.example.hzq.zqb.bean.LoginBean;
import com.example.hzq.zqb.model.Model;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import static com.example.hzq.zqb.activity.MainActivity.loginflag;

/**
 * Created by hzq on 2017/7/6.
 */

public class LoginActivity extends AppCompatActivity implements View.OnClickListener {
    private EditText username,pwd;
    private Button login;
    public  static int u_id;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        username=(EditText)findViewById(R.id.username);
        pwd=(EditText)findViewById(R.id.pwd);
        login=(Button)findViewById(R.id.login);
        login.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.login:
                if (username.getText().toString().isEmpty()||pwd.getText().toString().isEmpty()){
                    Toast.makeText(LoginActivity.this,"用户名或密码为空",Toast.LENGTH_SHORT).show();
                    return;
                }
                Model model=new Model();
                Call<LoginBean> call=model.login(username.getText().toString(),pwd.getText().toString());
                call.enqueue(new Callback<LoginBean>() {
                    @Override
                    public void onResponse(Call<LoginBean> call, Response<LoginBean> response) {
                        if (response.isSuccessful() && response.body() != null) {
                            if (!response.body().getU_id().equals("0")){
                                u_id=Integer.parseInt(response.body().getU_id());
                                loginflag=true;
                                Toast.makeText(LoginActivity.this,"登录成功",Toast.LENGTH_SHORT).show();
                                finish();
                            }
                            else  Toast.makeText(LoginActivity.this,"用户名或密码错误",Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onFailure(Call<LoginBean> call, Throwable t) {
                        Toast.makeText(LoginActivity.this,"404.login",Toast.LENGTH_SHORT).show();
                    }
                });
        }
    }
}
